/**
 * Candidate number: 35834
 * "The process of preparing programs for a digital computer is especially attractive, 
 * not only because it can be economically and scientifically rewarding, but also 
 * because it can be an aesthetic experience, much like composing poetry or music."
 *      —Donald E. Knuth
 */
package tv.starfu.systems;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import tv.starfu.components.FutureTransform;
import tv.starfu.components.MovementStyle;
import tv.starfu.components.CollisionMesh;
import tv.starfu.components.Transform;

/**
 * Handles Player and Level.
 * [E]Player: Transform, Anima, Physics, 
 * [E]Level: Transform, Anima, Physics,
 * @author niadzi
 */
public class PhysicsSystem extends EntityProcessingSystem {
    
    private ComponentMapper<FutureTransform>    controlMapper;
    private ComponentMapper<MovementStyle>      movementMapper;
    private ComponentMapper<CollisionMesh>      colMeshMapper;
    private ComponentMapper<Transform>          transformMapper;
    
    private float tpf;
    
    public PhysicsSystem(float tpf) {
        super(FutureTransform.class, MovementStyle.class, CollisionMesh.class, Transform.class);
        this.tpf = tpf;
    }
    
    @Override
    public void initialize() {
        controlMapper       = new ComponentMapper<FutureTransform>(FutureTransform.class, world);
        movementMapper      = new ComponentMapper<MovementStyle>(MovementStyle.class, world);
        colMeshMapper       = new ComponentMapper<CollisionMesh>(CollisionMesh.class, world);
        transformMapper     = new ComponentMapper<Transform>(Transform.class, world);
        
    }
    
    @Override
    protected void process(Entity e) {
        FutureTransform controller      = controlMapper.get(e);
        MovementStyle movement          = movementMapper.get(e);
        CollisionMesh collision         = colMeshMapper.get(e);
        Transform transform             = transformMapper.get(e);
        
//        // UPDATE TRANSFORM POSITION AND ORIENTATION WITH CONTROLLER DATA
//        transform.position =
//                transform.position.add(controller.velocity.mult(tpf));
//        transform.orientation += controller.rotation * tpf;
//                
//        // UPDATE CONTROLLER VELOCITY AND ROTATION
//        // WHERE ELSE ARE CONTROLLER VELOCITY AND ROTATION BEING MODIFIED?
//        controller.velocity =
//                controller.velocity.add(controller.linearSteering.mult(tpf));
//        controller.orientation += controller.angularSteering * tpf;
        
        /* OR USE....SLERRRP (when youve figured those damn quaternions out) */
           // transform.qRotator.slerp(controller.qRotator, 1);
          //  transform.position = controller.position;
      
//        // UPDATE TRANSFORM POSITION AND ORIENTATION WITH CONTROLLER DATA
//        transform.position =
//                transform.position.add(controller.velocity.mult(controller.linearSteering.mult(tpf*0.5f)));
//        transform.orientation += 
//                
//        // UPDATE CONTROLLER VELOCITY AND ROTATION
//        // WHERE ELSE ARE CONTROLLER VELOCITY AND ROTATION BEING MODIFIED?
//                controller.rotation * tpf * 0.5 * controller.angularSteering * tpf * 0.5f;
//        controller.velocity =
//                controller.velocity.add(controller.linearSteering.mult(tpf*0.5f));
    }
    
}
